//
//  CustomNativeView.swift
//  Runner
//
//  Created by Yuen sophie on 2019/12/20.
//  Copyright © 2019 The Chromium Authors. All rights reserved.
//

import UIKit
import Flutter

class CustomNativeViewPlugin {
    static func registerWith(registry:FlutterPluginRegistry) {
        let pluginKey = "Google_MapView_Plugin";
        if (registry.hasPlugin(pluginKey)) {return};
        let registrar = registry.registrar(forPlugin: pluginKey);
        let messenger = registrar.messenger() as! (NSObject & FlutterBinaryMessenger)
        registrar.register(CustomNativeView(messager:messenger),withId: "GoogleMapView");
    }
}

public class CustomNativeView: NSObject, FlutterPlatformViewFactory {
    public func create(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?) -> FlutterPlatformView {
        return CustomButton(withFrame: frame, viewIdentifier: viewId, arguments: args,binaryMessenger: messager)
    }
    
    var messager: FlutterBinaryMessenger!
    
    public init(messager: (NSObject & FlutterBinaryMessenger)?) {
        super.init()
        self.messager = messager
    }
    
    public func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
        return FlutterStandardMessageCodec.sharedInstance()
    }
}

public class CustomButton: NSObject, FlutterPlatformView {
    
    var cusFrame: CGRect!
    fileprivate var viewId: Int64!;
    fileprivate var channel: FlutterMethodChannel!
    
    public init(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?, binaryMessenger: FlutterBinaryMessenger) {
        super.init()

        self.viewId = viewId
        self.cusFrame = frame
        self.channel = FlutterMethodChannel(name: "samples.flutter.io/googleMap", binaryMessenger: binaryMessenger)
        self.channel.setMethodCallHandler({
                    (call: FlutterMethodCall, result: FlutterResult) -> Void in
                });
    }
    public func view() -> UIView {
        let button = UIButton(type: .roundedRect)
        button.frame = cusFrame
        button.setTitle("这就是原生的按钮", for: .normal)
        return button
    }
    
}
